// pages/personal/manageAdress/editAdree/editAdree.js
import api from '../../../../utils/api'
const apis = api.api
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		checked: false,
		name: null,
		mobile: null,
		userName: null,
		userMobile: null,
		mobileInp: null,
		addrInp: null,
		addrXxInp: null,
		nameInp: null,
		default: 0,
		provAddr: '',
		allAddr: '',
		addressId: 0,
	},
	onChange(event) {
		// event.detail 为当前输入的值
		console.log(event.detail, this.data.default)
		var that = this;
		if (event.detail == true) {
			that.setData({
				default: 1,
				checked: true,
			})
		} else {
			that.setData({
				default: 0,
				checked: false,
			})
		}

	},
	nameInp(e) {
		this.setData({
			nameInp: e.detail.value
		})
	},
	addrInp(e) {
		this.setData({
			addrInp: e.detail.value
		})
	},
	addrXxInp(e) {
		this.setData({
			addrXxInp: e.detail.value
		})
	},
	mobileInp(e) {
		this.setData({
			mobileInp: e.detail.value
		})
	},
	edit() {
		// https://dageda.api.hjdz.ltd/member/edit_address
		var userName = this.data.nameInp;
		var mobile = this.data.mobileInp;
		var prov = this.data.addrInp;
		var cont = this.data.addrXxInp;
		var address = this.data.addrInp + ' ' + this.data.addrXxInp
		console.log(prov, cont)
		if (userName == null) {
			userName = this.data.name;
		} else {
			userName = userName
		}
		if (mobile == null) {
			mobile = this.data.mobile;
		} else {
			mobile = mobile
		}
		if (prov == null || cont == null) {
			address = this.data.provAddr + ' ' + this.data.allAddr;
		} else {
			address = address
		}
		console.log('电话', mobile, userName, address)
		var phonetel = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
		var name = /^[u4E00-u9FA5]+$/;
		if (userName == '') {
			wx.showToast({
				title: '请输入用户名',
				icon: 'succes',
				duration: 1000,
				mask: true
			})
			return false
		} else if (mobile == '') {
			wx.showToast({
				title: '手机号不能为空',
			})
			return false
		} else if (mobile.length != 11) {
			wx.showToast({
				title: '手机号长度有误！',
				icon: 'success',
				duration: 1500
			})
			return false;
		}

		var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
		if (!myreg.test(mobile)) {
			wx.showToast({
				title: '手机号有误！',
				icon: 'success',
				duration: 1500
			})
			return false;
		} else {
			let that = this
			let url = apis.host + 'member/edit_address';
			let data = {
				mobile: mobile,
				/* 是	string	无 */
				address: address,
				/* 是	string	无 */
				name: userName,
				/* 是	string	无 */
				is_default: that.data.default,
				/* 否	string	默认0 0非默认 1默认地址 */
				id: that.data.addressId,
				/* 是	string	地址ID */
			}
			console.log('传的数据', data)
			apis.request(url, data, 'POST').then(function(res) {
				console.log('新增成功', res)
				wx.showToast({
					title: '新增成功',
					icon: 'success'
				})
				wx.navigateTo({
					url: '/pages/personal/manageAdress/index/index'
				})
			}).catch(function(res) {
				console.log('新增失败', res)
			})
		}
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var that = this;
		if (options.default == 0) {
			that.setData({
				checked: false
			})
		} else {
			that.setData({
				checked: true
			})
		}
		that.setData({
			name: decodeURI(options.userName),
			addressId: options.adressId,
			mobile: options.userMobile,
			default: parseInt(options.default),
			provAddr: decodeURI(options.address).trim().split(/\s+/)[0],
			allAddr: decodeURI(options.address).trim().split(/\s+/)[1],
		})
		console.log(options.adressId, options.default,
			decodeURI(options.userName), options.userMobile,
			decodeURI(options.userAdress))
	},
})
